import { defineStore } from 'pinia'

/**
 * 登录用户状态管理
 * 
 * */
export const tabs = defineStore('tabsStore', {
    state: () => ({
        tabsInfos: [{ title: "首页", name: 999999, closed: true, path: "Home" }], //标签页信息
        currentId:0,//当前的id
    }),
    getters: {
        //获取
        getTabsArr: (state) => {
            return state.tabsInfos;
        },
         //获取删除后的id
        getTabsCurrentId: (state) => {
            return state.currentId;
        },
    },
    actions: {
        //添加
        setTabsArr(obj) {
            // console.log(obj, "添加");
            this.tabsInfos.push(obj);
        },
        //删除选项卡
        removeTabs(obj) {
            const tabs = this.tabsInfos;
            let activeName = obj.activeName;
            if (activeName === obj.targetName) {
                tabs.forEach((tab, index) => {
                    if (tab.name === obj.targetName) {
                        const nextTab = tabs[index + 1] || tabs[index - 1];
                        if (nextTab) {
                            activeName = nextTab.name;
                        }
                    }
                });
            }

           this.currentId = activeName;
           this.tabsInfos = tabs.filter((tab) => tab.name !== obj.targetName);
        },
    },
    // 状态持久化
    //   persist: {
    //     enabled: true,
    //     strategies: [{
    //       key: 'tabsStore',
    //       storage: sessionStorage
    //     }]
    //   }
})